﻿/*
 * Copyright © 2025−2026 WEI.ZHOU (Willis) (Chengdu). All rights reserved.
 *
 * THIS SOURCE CODE IS PROPRIETARY AND CONFIDENTIAL.
 * It is the exclusive property of WEI.ZHOU (Willis) and may not be
 * used, copied, distributed, or disclosed outside of WEI.ZHOU (Willis)
 * without express written permission.
 *
 * This code is provided to WEI.ZHOU (Willis) under a limited, 
 * non-transferable internal license, and is for use only with the author's 
 * approved purposes. No external license is granted or implied.
 *
 * Any unauthorized use, reproduction, or distribution is strictly prohibited
 * and may result in severe civil and criminal penalties.
 */
 
using Microsoft.Extensions.Configuration;
using System.Data;

namespace Aore.Core.Factorys.DbConnection;

public class DatabaseConnectionFactory : IDbConnectionFactory
{
  private readonly IConfiguration _configuration;

  /// <summary>
  /// 依赖注入配置对象
  /// </summary>
  /// <param name="configuration"></param>
  public DatabaseConnectionFactory(IConfiguration configuration)
  {
    _configuration = configuration;
  }

  public IDbConnectionFactory CreateFactory()
  {
    var provider = _configuration["DatabaseConfig:DatabaseProvider"];
    string? connectionString = _configuration[$"DatabaseConfig:ConnectionStrings:{provider}"];

    return provider?.ToLower() switch
    {
      "oracle" => new OracleConnectionFactory(connectionString),
      "mysql" => new MySqlConnectionFactory(connectionString),
      "sqlserver" => new SqlServerConnectionFactory(connectionString),
      _ => throw new NotSupportedException($"Database {provider} not supported.")
    };
  }

  /// <summary>
  /// 创建数据库连接
  /// </summary>
  public IDbConnection CreateConnection()
  {
    return CreateFactory().CreateConnection();
  }
}